import sys
from PySide import QtGui, QtCore
from ledWidget import LedWidget

class DroWidget(QtGui.QWidget):

	def __init__(self, mainApp=None):
		super(DroWidget, self).__init__()
		
		self.mainApp = mainApp
		self.emcStat = self.mainApp.getEmcStat()
		self.initUI()
		
	def initUI(self):
		
		self.btnHomeAll = QtGui.QPushButton(QtGui.QIcon("/home/andris/aScreen/resources/home.svg"), "", self)
		self.btnHomeAll.setFixedSize(64, 200)
		self.btnHomeAll.clicked.connect(self.homeAllAxis)
		
		self.btnHomeX = QtGui.QPushButton("HOME\nX", self)
		self.btnHomeX.setFixedSize(64, 64)
		self.btnHomeX.clicked.connect(self.homeAxis)
		
		self.btnHomeY = QtGui.QPushButton("HOME\nY", self)
		self.btnHomeY.setFixedSize(64, 64)
		self.btnHomeY.clicked.connect(self.homeAxis)
		
		self.btnHomeZ = QtGui.QPushButton("HOME\nZ", self)
		self.btnHomeZ.setFixedSize(64, 64)
		self.btnHomeZ.clicked.connect(self.homeAxis)
		
		self.ledX = LedWidget()
		self.ledX.setFixedSize(64, 64)
		
		self.ledY = LedWidget()
		self.ledY.setFixedSize(64, 64)
		
		self.ledZ = LedWidget()
		self.ledZ.setFixedSize(64, 64)
		
		self.lcdX = QtGui.QLCDNumber()
		self.lcdX.setMinimumWidth(192)
		self.lcdX.setDigitCount(7)	
		
		self.lcdY = QtGui.QLCDNumber()
		self.lcdY.setMinimumWidth(192)
		self.lcdY.setDigitCount(7)	
		
		self.lcdZ = QtGui.QLCDNumber()
		self.lcdZ.setMinimumWidth(192)
		self.lcdZ.setDigitCount(7)
		
		self.btnSetMachineCoord = QtGui.QPushButton("Machine coord's", self)
		self.btnSetMachineCoord.setCheckable(True)
		
		#self.btnSetMachineCoord.setFixedSize(98, 48)		
		
		self.grid = QtGui.QGridLayout()
		self.grid.setContentsMargins(2, 2, 2, 2)
		self.grid.setSpacing(2)
		
		self.grid.addWidget(self.btnHomeAll, 1, 0, 3, 1)
		self.grid.addWidget(self.btnHomeX, 1, 1)
		self.grid.addWidget(self.btnHomeY, 2, 1)
		self.grid.addWidget(self.btnHomeZ, 3, 1)
		self.grid.addWidget(self.lcdX, 1, 2, 1, 3)
		self.grid.addWidget(self.lcdY, 2, 2, 1, 3)
		self.grid.addWidget(self.lcdZ, 3, 2, 1, 3)
		self.grid.addWidget(self.ledX, 1, 5)
		self.grid.addWidget(self.ledY, 2, 5)
		self.grid.addWidget(self.ledZ, 3, 5)
		self.grid.addWidget(self.btnSetMachineCoord, 4, 0, 1, 3)

		self.setLayout(self.grid)
		
	def updateData(self):
		self.xpos = 0
		self.ypos = 0
		self.zpos = 0
	
		if not self.btnSetMachineCoord.isChecked():
			self.xpos = self.emcStat.actual_position[0] - self.emcStat.g5x_offset[0]
			self.ypos = self.emcStat.actual_position[1] - self.emcStat.g5x_offset[1]
			self.zpos = self.emcStat.actual_position[2] - self.emcStat.g5x_offset[2]
		elif self.btnSetMachineCoord.isChecked():
			self.xpos = self.emcStat.actual_position[0]
			self.ypos = self.emcStat.actual_position[1]
			self.zpos = self.emcStat.actual_position[2]
		
		self.lcdX.display("%.3f" % self.xpos)
		self.lcdY.display("%.3f" % self.ypos)
		self.lcdZ.display("%.3f" % self.zpos)
		
		self.ledX.setState(self.emcStat.homed[0])
		self.ledY.setState(self.emcStat.homed[1])
		self.ledZ.setState(self.emcStat.homed[2])
		
	def homeAllAxis(self):
		self.mainApp.homeAllAxis()
		
	def homeAxis(self):
		sender = self.sender()
		if sender == self.btnHomeX:
			self.mainApp.homeAxis(0)
		elif sender == self.btnHomeY:
			self.mainApp.homeAxis(1)
		elif sender == self.btnHomeZ:
			self.mainApp.homeAxis(2)
		
